package com.eastdigit.system.model;

import com.eastdigit.system.model.Entity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by acer on 15-1-12.
 */
public abstract class TreeEntity extends Entity {

    private List<TreeEntity> children = new ArrayList<TreeEntity>();

    public abstract String getId();

    public abstract String getName();

    // public abstract int getLevel();
    //
    // public abstract void setLevel(int level);

    public abstract int getOrder();

    public abstract void setOrder(int order);

    public abstract String getParentId();

    public abstract void setParentId(String parentId);

    public List<TreeEntity> getChildren() {
        return children;
    }

    public void addChild(TreeEntity tree) {
        tree.setParentId(this.getId());
        children.add(tree);
    }

    public void addChild(TreeEntity tree, int index) {
        tree.setParentId(this.getId());
        if (index >= children.size()) {
            children.add(tree);
        } else {
            List<TreeEntity> list = new ArrayList<TreeEntity>();
            for (int i = 0; i < children.size(); i++) {
                if (i == index) {
                    list.add(tree);
                }
                list.add(children.get(i));
            }
            children.clear();
            children.addAll(list);
        }
    }

    public void removeChild(TreeEntity tree) {
        children.remove(tree);
    }

    public void setParent(TreeEntity tree) {
        this.setParentId(tree.getId());
        tree.addChild(this);
    }
}
